package com.five.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.admin.dto.AdminThreadLocalDto;
import com.five.admin.dto.RoleQueryDto;
import com.five.admin.dto.RoleUpdateMenuDto;
import com.five.admin.server.RoleService;
import com.five.admin.threadLocal.AdminThreadLocal;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.core.utils.JsonUtils;
import com.five.pojo.dto.BatchIds;
import com.five.pojo.entity.Role;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestBody RoleQueryDto queryDto) {
        //准备分页条件
        Page<Role> page = new Page<>(queryDto.getPage(), queryDto.getLimit());
        //准备查询条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like((queryDto.getName() != null && queryDto.getName() != ""), Role::getName, queryDto.getName());

        //查询数据
        Page<Role> rolePage = roleService.page(page, queryWrapper);

        if (rolePage.getTotal()==0){
            return Result.buildSuccess(ResultEnum.DATA_IS_NULL);
        }
        return Result.buildSuccess(rolePage.getTotal(),rolePage.getRecords());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody @Valid Role role) {
        AdminThreadLocalDto adminThreadLocalDto = AdminThreadLocal.get();
        role.setCreator(adminThreadLocalDto.getUsername());
        if (roleService.save(role)) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.ADD_FALSE);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.UPDATE_FALSE);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Long id) {
        roleService.deleteRoleMenu(id);
        if (roleService.removeById(id)) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.DELETE_FALSE);
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result deleteBatch(@RequestBody BatchIds batchIds) {
        batchIds.getIds().forEach(id->{
            roleService.deleteRoleMenu(id);
        });
        if (roleService.removeByIds(batchIds.getIds())) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.DELETE_FALSE);
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Result getAll() {
        return Result.buildSuccess(roleService.list());
    }

    @RequestMapping(value = "/findByAdminId", method = RequestMethod.GET)
    public Result findByAdminId(Long id) {
        System.err.println(id);
        List<Long> roles = roleService.findByAdminId(id);
        return Result.buildSuccess(roles);
    }

    @PostMapping("/UpdateRoleMenuByRoleId")
    public Result UpdateRoleMenuByRoleId(@RequestBody RoleUpdateMenuDto roleUpdateMenuDto) {
        int i = roleService.updateRoleAndMenu(roleUpdateMenuDto);
        return i >= 1 ? Result.buildSuccess(ResultEnum.UPDATE_TRUE) : Result.buildFail(ResultEnum.UPDATE_FALSE);
    }
}
